//
//  AwesomeIntroGuideView+SLB.m
//  Pods
//
//  Created by Senmiao on 2017/3/27.
//
//

#import "AwesomeIntroGuideView+SLB.h"
#import <BlocksKit/BlocksKit+UIKit.h>
#import <ReactiveObjC/RACEXTScope.h>
#import <MGJRouter/MGJRouter.h>
#import <objc/runtime.h>

#define SLB_SCREENHEIGHT 320
#define SLB_SCREENWIDTH 320

NSUInteger markIndex;

@implementation AwesomeIntroGuideView (SLB)

- (void)testMethod {
    
}

- (void)setCameraBlock:(SLBCameraBlock)cameraBlock {
    objc_setAssociatedObject(self, "cameraBlock", cameraBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (SLBCameraBlock)cameraBlock {
    return (void (^)(void))objc_getAssociatedObject(self, "cameraBlock");
}

static char kAssociatedObjectKey_SLBTESTARRAY;
- (void)setTestArray:(NSArray *)argv {
    objc_setAssociatedObject(self, &kAssociatedObjectKey_SLBTESTARRAY, argv, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (NSArray *)testArray {
    return (NSArray *)objc_getAssociatedObject(self, &kAssociatedObjectKey_SLBTESTARRAY);
}

- (void)addActionForView:(UIView *)view {
    @weakify(self);
    self.willNavgateBlock = ^(AwesomeIntroGuideView * _Nonnull guideView, NSUInteger indedx) {
        markIndex = indedx;
    };
    view.userInteractionEnabled = YES;
    
    NSMethodSignature *sig = [AwesomeIntroGuideView instanceMethodSignatureForSelector:@selector(goToCoachMarkIndexed:)];
    NSInvocation __block *invocation = [NSInvocation invocationWithMethodSignature:sig];
    invocation.target = self;
    invocation.selector = @selector(goToCoachMarkIndexed:);
    
    [view addGestureRecognizer:({
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] bk_initWithHandler:^(UIGestureRecognizer *sender, UIGestureRecognizerState state, CGPoint location) {
            @strongify(self);
            if (markIndex == 0) {
                CGRect frame = CGRectMake(0,SLB_SCREENHEIGHT/2 - 60, SLB_SCREENWIDTH, 120);
                if (CGRectContainsPoint(frame, location)) {
                    [self responseUrl];
                }else {
                    [invocation setArgument:&markIndex + 1 atIndex:2];
                }
            }else if (markIndex == 1) {
                CGRect frame = CGRectMake(SLB_SCREENWIDTH - 9 -100,SLB_SCREENWIDTH/15*13*220/359, 140, 120);
                if (CGRectContainsPoint(frame, location)) {
                    [self responseUrl];
                }else {
                    NSUInteger index = markIndex + 1;
                    [invocation setArgument:&index atIndex:2];
                }
            }else if (markIndex == 2) {
                CGRect frame = CGRectMake(SLB_SCREENWIDTH - 9 -100,0, 140, 120);
                if (CGRectContainsPoint(frame, location)) {
                    !self.cameraBlock?:self.cameraBlock();
                    NSUInteger index = markIndex + 1;
                    [invocation setArgument:&index atIndex:2];
                }else {
                    NSUInteger index = markIndex + 1;
                    [invocation setArgument:&index atIndex:2];
                }
            }
        }];
        tap;
    })];
}

- (void)responseUrl {
    if ([self valueForKey:@"redirectURL"]) {
        BOOL open = [MGJRouter canOpenURL:[self valueForKey:@"redirectURL"]];
        if (open) {
            [MGJRouter openURL:[self valueForKey:@"redirectURL"]];
        } else {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[self valueForKey:@"redirectURL"]]];
        }
    }

}

- (void)animateGuideImgae:(UIImage *)guideImage withPoint:(CGPoint)point {
    self.guideImageView.image = guideImage;
    [self.guideImageView sizeToFit];
    if (self.autoCalculateGuidePoint) {
        CGFloat scale = [UIScreen mainScreen].bounds.size.width / 375;
        CGFloat X = point.x;
        X = (X/scale + self.guideImageView.frame.size.width)*scale - self.guideImageView.frame.size.width;
        point = CGPointMake(X, point.y);
    }
    [self addActionForView:self.guideImageView];
    self.guideImageView.frame = (CGRect) {point,[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height};
}

@end
